package com.lehecai.core.api.message;

import com.lehecai.core.api.AbstractApiResultBean;
import com.lehecai.core.util.CoreDateUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by zhangzq on 14-4-11.
 */
public class UserMessage extends AbstractApiResultBean {

    public static final String QUERY_ID = "id";
    public static final String QUERY_UID = "uid";
    public static final String QUERY_MID = "mid";
    public static final String QUERY_CLASSES = "classes";
    public static final String QUERY_TYPE = "type";
    public static final String QUERY_CREATEAT = "create_at";

    public static final String SET_ID = "id";
    public static final String SET_ISREAD = "is_read";

    private String id;
    private String mid;                    //消息id
    private String uid;                   //消息标题
    private Date createAt;                //有效时间
    private MessageType classes;			//消息类型
    private MessageDetailType type;			//消息类别
    private IsReadType isRead;             //是否推送
    private String userName;

    public static UserMessage convertFromJSONObject(JSONObject object) {
        if (object == null) {
            return null;
        }
        UserMessage userMessage = new UserMessage();
        userMessage.id = getString(object, "id");
        userMessage.uid = getString(object, "uid");
        userMessage.mid = getString(object, "mid");
        userMessage.createAt = CoreDateUtils.parseDate(("".equals(getString(object, "create_at")) || "0000-00-00 00:00:00".equals(getString(object, "create_at"))) ? null : getString(object, "create_at"), CoreDateUtils.DATETIME);
        userMessage.classes = MessageType.getItem(object.getInt("classes"));
        userMessage.type = MessageDetailType.getItem(object.getInt("type"));
        userMessage.isRead = IsReadType.getItem(object.getInt("is_read"));
        userMessage.userName = getString(object, "userName");
        return userMessage;
    }

    public static List<UserMessage> convertFromJSONArray(JSONArray array) {
        if (array == null) {
            return null;
        }
        List<UserMessage> list = new ArrayList<UserMessage>();
        for (Iterator<?> iterator = array.iterator(); iterator.hasNext();) {
            JSONObject object = (JSONObject) iterator.next();
            list.add(convertFromJSONObject(object));
        }
        return list;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMid() {
        return mid;
    }

    public void setMid(String mid) {
        this.mid = mid;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public Date getCreateAt() {
        return createAt;
    }

    public void setCreateAt(Date createAt) {
        this.createAt = createAt;
    }

    public MessageType getClasses() {
        return classes;
    }

    public void setClasses(MessageType classes) {
        this.classes = classes;
    }

    public MessageDetailType getType() {
        return type;
    }

    public void setType(MessageDetailType type) {
        this.type = type;
    }

    public IsReadType getIsRead() {
        return isRead;
    }

    public void setIsRead(IsReadType isRead) {
        this.isRead = isRead;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
